package email;

import java.util.ArrayList;
import java.util.Date;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.swing.table.AbstractTableModel;

// This class manages the e-mail table's data.
public class MessagesTableModel extends AbstractTableModel {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// These are the names for the table's columns.
    private static final String[] columnNames = {"Sender",
    "Subject", "Date"};
    
    // The table's list of messages.
    private ArrayList messageList = new ArrayList();
    
    // Sets the table's list of messages.
    public void setMessages(Message[] messages) {
        for (int i = messages.length - 1; i >= 0; i--) {
            messageList.add(messages[i]);
        }
        
        // Fire table data change notification to table.
        fireTableDataChanged();
    }
    
    // Get a message for the specified row.
    public Message getMessage(int row) {
        return (Message) messageList.get(row);
    }
    
    public void setMessageSubject(int row, String subject){
    	MimeMessage m = (MimeMessage)messageList.get(row);
    	try {
			m.setSubject(subject);
			System.out.println(m.getSubject());
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	messageList.remove(row);
    	messageList.add(row, m);
    	fireTableRowsUpdated(row, row);
    }
    
    // Remove a message from the list.
    public void deleteMessage(int row) {
        messageList.remove(row);
        
        // Fire table row deletion notification to table.
        fireTableRowsDeleted(row, row);
    }
    
    public void deleteMessage(ReceivedMessage message){
    	long ourmessageid = Util.createId(message.getSubject()+message.getBody());
    	
    	for(int i = 0; i < messageList.size(); i++){
    		Message m = (Message) messageList.get(i);
    		long themessageid = Util.createId(m);
			if(themessageid == ourmessageid){
				messageList.remove(i);
				fireTableRowsDeleted(i, i);
				break;
			}
    	}
    	
    }
    
    
    // Get table's column count.
    public int getColumnCount() {
        return columnNames.length;
    }
    
    // Get a column's name.
    public String getColumnName(int col) {
        return columnNames[col];
    }
    
    // Get table's row count.
    public int getRowCount() {
        return messageList.size();
    }
    
    // Get value for a specific row and column combination.
    public Object getValueAt(int row, int col) {
        try {
            Message message = (Message) messageList.get(row);
            switch (col) {
                case 0: // Sender
                    Address[] senders = message.getFrom();
                    if (senders != null || senders.length > 0) {
                        return senders[0].toString();
                    } else {
                        return "[none]";
                    }
                case 1: // Subject
                    String subject = message.getSubject();
                    if (subject != null && subject.length() > 0) {
                        return subject;
                    } else {
                        return "[none]";
                    }
                case 2: // Date
                    Date date = message.getSentDate();
                    if (date != null) {
                        return date.toString();
                    } else {
                        return "[none]";
                    }
            }
        } catch (Exception e) {
            // Fail silently.
            return "";
        }
        
        return "";
    }
}